package com.itheima.mybatis.config;

import com.itheima.mybatis.dao.UserMapper;
import com.itheima.mybatis.framework.DefaultHmSqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;


/**
 * @author ：sean
 * @date ：Created in 2020/6/19
 * @description ：
 * @version: 1.0
 */

@EnableAspectJAutoProxy
@Configuration
@ComponentScan("com.itheima.mybatis")
@MapperScan("com.itheima.mybatis.dao")
public class HmMybaitsConfig {

	@Bean
	public DataSource dataSource(){
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUsername("root");
		dataSource.setPassword("root");
		dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/itheima_mm?characterEncoding=utf-8");
		return dataSource;
	}
	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource());
		System.out.println("sqlSessionFactory");
		return factoryBean.getObject();
	}

	@Bean
	public UserMapper userMapper(){
		DefaultHmSqlSession defaultHmSqlSession = new DefaultHmSqlSession();
		return defaultHmSqlSession.getMapper(UserMapper.class);
	}


}
